package com.is2.patchinglife.activity;

import com.is2.patchinglife.R;
import com.is2.patchinglife.manager.ActivityManager;
import com.is2.patchinglife.manager.RemoteDataManager;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;


public class SignUpActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		// shows activity interface
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_signup);

		//Add activity to manager
		ActivityManager.getInstance().addActivity("SignUpActivity", this);
		
		//Listner Checkbox Import Trophy
		final CheckBox importCheckbox = (CheckBox) findViewById(R.id.importCheck);
		importCheckbox.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				if(importCheckbox.isChecked()){
					showAlert("Remember, if the import box is checked, the GUEST user Trophy will be imported to the new user and deleted from the GUEST Account");
				}
			}
		});
		FrameLayout register = (FrameLayout) findViewById(R.id.signupRegisterBtn);
		register.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				ActivityManager.executeButtonEffects(); 	// execute button effects
				
				// Get Reference to user box
				EditText signupUser = (EditText) findViewById(R.id.signupUser);
				EditText signupPwd = (EditText) findViewById(R.id.signupPwd);
				EditText signupPwdCheck = (EditText) findViewById(R.id.signupPwdCheck);
				//get data
				String user = signupUser.getText().toString();
				String psw = signupPwd.getText().toString();
				String pswCheck = signupPwdCheck.getText().toString();
				
				// Check validity
				if(user.equals("") || psw.equals("")||  pswCheck.equals("")){
					//Alert Form not filled
					showAlert("You must fill every field!");
				// psw must be same
				}else if( !psw.equals(pswCheck)){
					
					showAlert("The passwords and confirm password must be the same!");
				}else{
					if(RemoteDataManager.isOnline(SignUpActivity.this))
						RemoteDataManager.userSubscribe(user, psw, pswCheck, importCheckbox.isChecked());
				}
			}
		});
		
	}
	
	/** Display an AlertDialog with the given message**/
	public void showAlert(String _msg){

		
		AlertDialog.Builder builder=new AlertDialog.Builder(SignUpActivity.this);
		builder.setTitle("ALERT");
		builder.setCancelable(true);
		builder.setPositiveButton("OK", null);
		builder.setMessage(_msg);
		AlertDialog alert=builder.create();
		alert.show();
		
	}
	 @Override
    public void onDestroy(){
	 String[] aList = {"SignUpActivity"};
	 ActivityManager.getInstance().closeActivities(aList,false);
	 
        super.onDestroy();
    }
}
